package com.yglab.sqpoll.android.login;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.yglab.sqpoll.android.R;
import com.yglab.sqpoll.android.settings.*;
import com.yglab.sqpoll.android.splash.SplashActivity;
/***
 * login SQpoll by facebook
 * @author user HongGyun Bae
 *
 */
public class LoginFacebook extends Activity {

	public static final String TAG = "SampleFacebookAppActivity";
	
	Button mConnectButton;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.faceapp_main);
        
        //startActivity(new Intent(this, SplashActivity.class));

        mConnectButton = (Button) findViewById(R.id.connectBtn);
        mConnectButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		connect();
        	}
        });
       
    }


    @SuppressWarnings("deprecation")
	private void connect() {
    	try {
    		Facebook facebook = new Facebook(BasicInfo.FACEBOOK_APP_ID);
       		BasicInfo.FacebookInstance = facebook;

    		if (!BasicInfo.RetryLogin && BasicInfo.FacebookLogin == true) {
    			facebook.setAccessToken(BasicInfo.FACEBOOK_ACCESS_TOKEN);
    			
    		} else {
    			facebook.authorize(this, BasicInfo.FACEBOOK_PERMISSIONS, new AuthorizationListener());
    		}
    	} catch (Exception ex) {
			ex.printStackTrace();
		}
    }

	@SuppressWarnings("deprecation")
	protected void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
		super.onActivityResult(requestCode, resultCode, resultIntent);

		if (resultCode == RESULT_OK) {
			if (requestCode == 32665)
			{
				BasicInfo.FacebookInstance.authorizeCallback(requestCode, resultCode, resultIntent);
			}
		}
	}

	protected void onPause() {
		super.onPause();

		saveProperties();
	}

	protected void onResume() {
		super.onResume();

		loadProperties();

	}

	private void saveProperties() {
		SharedPreferences pref = getSharedPreferences("FACEBOOK", MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();

		editor.putBoolean("FacebookLogin", BasicInfo.FacebookLogin);
		editor.putString("FACEBOOK_ACCESS_TOKEN", BasicInfo.FACEBOOK_ACCESS_TOKEN);
		editor.putString("FACEBOOK_NAME", BasicInfo.FACEBOOK_NAME);

		editor.commit();
	}

	private void loadProperties() {
		SharedPreferences pref = getSharedPreferences("FACEBOOK", MODE_PRIVATE);

		BasicInfo.FacebookLogin = pref.getBoolean("FacebookLogin", false);
		BasicInfo.FACEBOOK_ACCESS_TOKEN = pref.getString("FACEBOOK_ACCESS_TOKEN", "");
		BasicInfo.FACEBOOK_NAME = pref.getString("FACEBOOK_NAME", "");

	}


	public class AuthorizationListener implements DialogListener {

		public void onCancel() {

		}

		@SuppressWarnings("deprecation")
		public void onComplete(Bundle values) {
			try{
				String resultStr = BasicInfo.FacebookInstance.request("me");
				JSONObject obj = new JSONObject(resultStr);
								
				BasicInfo.FACEBOOK_NAME = obj.getString("name");
				BasicInfo.FacebookLogin = true;
				BasicInfo.FACEBOOK_ACCESS_TOKEN = BasicInfo.FacebookInstance.getAccessToken();
			
			} catch(Exception ex){
				ex.printStackTrace();
			} 
		
			Log.d(TAG, "Authorization completed : " + BasicInfo.FACEBOOK_NAME);
			
			Toast.makeText(getBaseContext(), BasicInfo.FACEBOOK_NAME, Toast.LENGTH_LONG).show();
			Intent intent = new Intent(getBaseContext(),SettingsActivity.class);
			startActivity(intent);			
		}


		public void onError(DialogError e) {

		}


		public void onFacebookError(FacebookError e) {

		}

	}
	
	

}

